import React from 'react';
import ProForm, {ProFormText} from "@ant-design/pro-form";
import {Form, message, Modal, TreeSelect} from "antd";
import {addPermission} from "@/services/permission";

const Create = (props) => {

  const {isModalVisible, isShowModal, treeData, actionRef} = props

  /**
   * 添加权限
   * @param values
   */
  const createPermission = async values => {
    if (values.pid === 0){
      values.pid = '';
    }
    const response = await addPermission(values)
    if (response.status === undefined) {
      message.success('添加成功')
      // 刷新表格数据
      actionRef.current.reload()
      // 关闭模态框
      isShowModal(false)
    }
  }

  return (
    <div>
      <Modal
        title="添加权限"
        footer={null}
        centered={true}
        visible={isModalVisible}
        onCancel={() => isShowModal(false)}
        destroyOnClose={true}
      >
        <ProForm onFinish={values => createPermission(values)}>
          <Form.Item name="pid" label="父级权限">
            <TreeSelect
              style={{width: 328}}
              placeholder="父级部门"
              allowClear
              treeData={treeData}
              rules={[{required: true, message: '请选择父级权限'}]}
            />
          </Form.Item>
          <ProFormText width="md" name="name" label="路由地址"
                       rules={[{required: true, message: '请输入路由地址'}]}
                       placeholder="请输入路由地址"/>
          <ProFormText width="md" name="cn_name" label="权限名称"
                       rules={[{required: true, message: '请输入权限名称'}]}
                       placeholder="请输入权限名称"/>
        </ProForm>
      </Modal>
    </div>
  );
};

export default Create;
